﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL;
namespace BLL
{
    public class HoaDon_BLL
    {
        DataTable dt = new DataTable();
        DataService DB = new DataService();
        public DataTable TinhTrangHD()
        {
            dt = DB.ViewTableParamStore("TinhTrangHD_Select");
            return dt;        
        }
        public DataTable PhuongThucTT_Select()
        {
            dt = DB.ViewTableParamStore("LoaiThanhToan_Select");
            return dt;
        }
        public DataTable HoaDon_ViewDuLieu(int Active, int HoaDonID)
        { 
             SqlParameter[] arrParam ={
                new SqlParameter("@Active", SqlDbType.Int),
                new SqlParameter("@HoaDonID",SqlDbType.Int)};
            arrParam[0].Value = Active;
            arrParam[1].Value = HoaDonID;
            dt=DB.ViewTableParamStore("HoaDon_ViewDL", arrParam);
            return dt;        
        }
        public DataTable HoaDon_UserView(string MaKH)
        {
            SqlParameter[] arrParam ={
                new SqlParameter("@MaKH", SqlDbType.VarChar)};
            arrParam[0].Value = MaKH;            
            dt = DB.ViewTableParamStore("HoaDon_UserView", arrParam);
            return dt;     
        }
        public void HoaDon_Edit(int HoaDonID, int MaKH, string Nhan_GioiTinh, string Nhan_HoTen ,
                string Nhan_CongTy, string Nhan_DiaChi, string Nhan_DienThoai, string Nhan_ThanhPho,
               string Nhan_HuyenTinh, string Nhan_MaVung, string Nhan_TenNuoc, string Tra_GioiTinh, string Tra_HoTen,
               string Tra_CongTy, string Tra_DiaChi, string Tra_DienThoai, string Tra_ThanhPho, string Tra_HuyenTinh,
                string Tra_MaVung, string Tra_TenNuoc, int PTThanhToanID, string Comment, string AdminComment,
                 DateTime NgayLap, float TongTien, int TinhTrangID, ref  string error)    
        {
            SqlParameter[] arrParam ={
                new SqlParameter("@Active", SqlDbType.Int),
                new SqlParameter("@HoaDonID", SqlDbType.Int),
                new SqlParameter("@MaKH", SqlDbType.Int),
                new SqlParameter("@Nhan_GioiTinh", SqlDbType.VarChar),
                new SqlParameter("@Nhan_HoTen", SqlDbType.NVarChar),
                new SqlParameter("@Nhan_CongTy",SqlDbType.NVarChar),     
                  new SqlParameter("@Nhan_DiaChi", SqlDbType.NVarChar),
                new SqlParameter("@Nhan_DienThoai", SqlDbType.VarChar),
                new SqlParameter("@Nhan_ThanhPho", SqlDbType.NVarChar),
                new SqlParameter("@Nhan_HuyenTinh", SqlDbType.NVarChar),
                new SqlParameter("@Nhan_MaVung", SqlDbType.VarChar),
                new SqlParameter("@Nhan_TenNuoc",SqlDbType.NVarChar),      
                  new SqlParameter("@Tra_GioiTinh", SqlDbType.VarChar),
                new SqlParameter("@Tra_HoTen", SqlDbType.NVarChar),
                new SqlParameter("@Tra_CongTy", SqlDbType.NVarChar),
                new SqlParameter("@Tra_DiaChi", SqlDbType.NVarChar),
                new SqlParameter("@Tra_DienThoai", SqlDbType.VarChar),
                new SqlParameter("@Tra_ThanhPho",SqlDbType.NVarChar),      
                  new SqlParameter("@Tra_HuyenTinh", SqlDbType.NVarChar),
                new SqlParameter("@Tra_MaVung", SqlDbType.VarChar),
                new SqlParameter("@Tra_TenNuoc", SqlDbType.NVarChar),
                new SqlParameter("@PTThanhToanID", SqlDbType.Int),
                new SqlParameter("@Comment", SqlDbType.NVarChar),
                new SqlParameter("@AdminComment",SqlDbType.NVarChar),      
                new SqlParameter("@NgayLap",SqlDbType.DateTime),
                 new SqlParameter("@TongTien",SqlDbType.Float),     
                new SqlParameter("@TinhTrangID",SqlDbType.Int),      
                new SqlParameter("@error",SqlDbType.NVarChar,200)};
            arrParam[0].Value = HoaDonID;
            arrParam[1].Value = MaKH;
            arrParam[2].Value = Nhan_GioiTinh;
            arrParam[3].Value = Nhan_HoTen;
            arrParam[4].Value = Nhan_CongTy;
            arrParam[5].Value = Nhan_DiaChi;
            arrParam[6].Value = Nhan_DienThoai;
            arrParam[7].Value = Nhan_ThanhPho;
            arrParam[8].Value = Nhan_HuyenTinh;
            arrParam[9].Value = Nhan_MaVung;
            arrParam[10].Value = Nhan_TenNuoc;
            arrParam[11].Value = Tra_GioiTinh;
            arrParam[12].Value = Tra_HoTen;
            arrParam[13].Value = Tra_CongTy;
            arrParam[14].Value = Tra_DiaChi;
            arrParam[15].Value = Tra_DienThoai;
            arrParam[16].Value = Tra_ThanhPho;
            arrParam[17].Value = Tra_HuyenTinh;
            arrParam[18].Value = Tra_MaVung;
            arrParam[19].Value = Tra_TenNuoc;
            arrParam[20].Value = PTThanhToanID;
            arrParam[21].Value = Comment;
            arrParam[22].Value = AdminComment;
            arrParam[23].Value = NgayLap;
            arrParam[24].Value = TongTien;
            arrParam[25].Value = TinhTrangID;
           
            arrParam[26].Direction = ParameterDirection.Output;//Luu y cho nay           
            DB.ExeCuteStoreParam("HoaDon_Edit", arrParam);
            error = (arrParam[26].Value.ToString() + "" != "" ? arrParam[26].Value.ToString() : "");
        }
        public void HoaDon_LuuThanhToan(int HoaDonID, string Tra_GioiTinh, string Tra_HoTen,
               string Tra_CongTy, string Tra_DiaChi, string Tra_DienThoai, string Tra_ThanhPho, string Tra_HuyenTinh,
                string Tra_MaVung, string Tra_TenNuoc, ref  string error)
        { 
         SqlParameter[] arrParam ={              
                new SqlParameter("@HoaDonID", SqlDbType.Int),
                new SqlParameter("@Tra_GioiTinh", SqlDbType.VarChar),
                new SqlParameter("@Tra_HoTen", SqlDbType.NVarChar),
                new SqlParameter("@Tra_CongTy", SqlDbType.NVarChar),
                new SqlParameter("@Tra_DiaChi", SqlDbType.NVarChar),
                new SqlParameter("@Tra_DienThoai", SqlDbType.VarChar),
                new SqlParameter("@Tra_ThanhPho",SqlDbType.NVarChar),      
                new SqlParameter("@Tra_HuyenTinh", SqlDbType.NVarChar),
                new SqlParameter("@Tra_MaVung", SqlDbType.VarChar),
                new SqlParameter("@Tra_TenNuoc", SqlDbType.NVarChar),
                new SqlParameter("@error",SqlDbType.NVarChar,200)};
         arrParam[0].Value = HoaDonID;       
         arrParam[1].Value = Tra_GioiTinh;
         arrParam[2].Value = Tra_HoTen;
         arrParam[3].Value = Tra_CongTy;
         arrParam[4].Value = Tra_DiaChi;
         arrParam[5].Value = Tra_DienThoai;
         arrParam[6].Value = Tra_ThanhPho;
         arrParam[7].Value = Tra_HuyenTinh;
         arrParam[8].Value = Tra_MaVung;
         arrParam[9].Value = Tra_TenNuoc;
         arrParam[10].Direction = ParameterDirection.Output;//Luu y cho nay           
         DB.ExeCuteStoreParam("HoaDon_LuuThanhToan", arrParam);
         error = (arrParam[10].Value.ToString() + "" != "" ? arrParam[10].Value.ToString() : "");
        }
        public void HoaDon_LuuNhan(int HoaDonID, string Nhan_GioiTinh, string Nhan_HoTen,
                string Nhan_CongTy, string Nhan_DiaChi, string Nhan_DienThoai, string Nhan_ThanhPho,
               string Nhan_HuyenTinh, string Nhan_MaVung, string Nhan_TenNuoc, ref  string error)
        {
            SqlParameter[] arrParam ={              
                new SqlParameter("@HoaDonID", SqlDbType.Int),               
                new SqlParameter("@Nhan_GioiTinh", SqlDbType.VarChar),
                new SqlParameter("@Nhan_HoTen", SqlDbType.NVarChar),
                new SqlParameter("@Nhan_CongTy",SqlDbType.NVarChar),     
                new SqlParameter("@Nhan_DiaChi", SqlDbType.NVarChar),
                new SqlParameter("@Nhan_DienThoai", SqlDbType.VarChar),
                new SqlParameter("@Nhan_ThanhPho", SqlDbType.NVarChar),
                new SqlParameter("@Nhan_HuyenTinh", SqlDbType.NVarChar),
                new SqlParameter("@Nhan_MaVung", SqlDbType.VarChar),
                new SqlParameter("@Nhan_TenNuoc",SqlDbType.NVarChar),     
                new SqlParameter("@error",SqlDbType.NVarChar,200)};
            arrParam[0].Value = HoaDonID;         
            arrParam[1].Value = Nhan_GioiTinh;
            arrParam[2].Value = Nhan_HoTen;
            arrParam[3].Value = Nhan_CongTy;
            arrParam[4].Value = Nhan_DiaChi;
            arrParam[5].Value = Nhan_DienThoai;
            arrParam[6].Value = Nhan_ThanhPho;
            arrParam[7].Value = Nhan_HuyenTinh;
            arrParam[8].Value = Nhan_MaVung;
            arrParam[9].Value = Nhan_TenNuoc;           
            arrParam[10].Direction = ParameterDirection.Output;//Luu y cho nay           
            DB.ExeCuteStoreParam("HoaDon_LuuNhan", arrParam);
            error = (arrParam[10].Value.ToString() + "" != "" ? arrParam[10].Value.ToString() : "");
        }
        public void HoaDon_LuuDropComment(int HoaDonID, int PTThanhToanID, string Comment, string AdminComment,
                 int TinhTrangID, ref  string error)
        {
            SqlParameter[] arrParam ={             
                new SqlParameter("@HoaDonID", SqlDbType.Int),              
                new SqlParameter("@PTThanhToanID", SqlDbType.Int),
                new SqlParameter("@Comment", SqlDbType.NVarChar),
                new SqlParameter("@AdminComment",SqlDbType.NVarChar),     
                new SqlParameter("@TinhTrangID",SqlDbType.Int),      
                new SqlParameter("@error",SqlDbType.NVarChar,200)};
            arrParam[0].Value = HoaDonID;        
            arrParam[1].Value = PTThanhToanID;
            arrParam[2].Value = Comment;
            arrParam[3].Value = AdminComment;        
            arrParam[4].Value = TinhTrangID;

            arrParam[5].Direction = ParameterDirection.Output;//Luu y cho nay           
            DB.ExeCuteStoreParam("HoaDon_LuuDropComment", arrParam);
            error = (arrParam[5].Value.ToString() + "" != "" ? arrParam[5].Value.ToString() : "");
        }
        public void HoaDon_UpdateAdminComment(int HoaDonID, string AdminComment, ref string error)
        { 
        SqlParameter[] arrParam ={              
                new SqlParameter("@HoaDonID", SqlDbType.Int),
                new SqlParameter("@AdminComment", SqlDbType.NVarChar),
                new SqlParameter("@error",SqlDbType.NVarChar,200)};
        arrParam[0].Value = HoaDonID;
        arrParam[1].Value = AdminComment;

        arrParam[2].Direction = ParameterDirection.Output;//Luu y cho nay           
        DB.ExeCuteStoreParam("HoaDon_UpdateAdminComment", arrParam);
        error = (arrParam[2].Value.ToString() + "" != "" ? arrParam[2].Value.ToString() : "");
        }
        public void HoaDon_ThemMoi(int MaKH, string Nhan_GioiTinh, string Nhan_HoTen,
               string Nhan_CongTy, string Nhan_DiaChi, string Nhan_DienThoai, string Nhan_ThanhPho,
              string Nhan_HuyenTinh, string Nhan_MaVung, string Nhan_TenNuoc, string Tra_GioiTinh, string Tra_HoTen,
              string Tra_CongTy, string Tra_DiaChi, string Tra_DienThoai, string Tra_ThanhPho, string Tra_HuyenTinh,
               string Tra_MaVung, string Tra_TenNuoc, int PTThanhToanID, string Comment, string AdminComment,
                DateTime NgayLap, float TongTien, int TinhTrangID, ref  string error, ref string MaHD)
        {
            SqlParameter[] arrParam ={
                new SqlParameter("@MaKH", SqlDbType.Int),
                new SqlParameter("@Nhan_GioiTinh", SqlDbType.VarChar),
                new SqlParameter("@Nhan_HoTen", SqlDbType.NVarChar),
                new SqlParameter("@Nhan_CongTy",SqlDbType.NVarChar),     
                  new SqlParameter("@Nhan_DiaChi", SqlDbType.NVarChar),
                new SqlParameter("@Nhan_DienThoai", SqlDbType.VarChar),
                new SqlParameter("@Nhan_ThanhPho", SqlDbType.NVarChar),
                new SqlParameter("@Nhan_HuyenTinh", SqlDbType.NVarChar),
                new SqlParameter("@Nhan_MaVung", SqlDbType.VarChar),
                new SqlParameter("@Nhan_TenNuoc",SqlDbType.NVarChar),      
                  new SqlParameter("@Tra_GioiTinh", SqlDbType.VarChar),
                new SqlParameter("@Tra_HoTen", SqlDbType.NVarChar),
                new SqlParameter("@Tra_CongTy", SqlDbType.NVarChar),
                new SqlParameter("@Tra_DiaChi", SqlDbType.NVarChar),
                new SqlParameter("@Tra_DienThoai", SqlDbType.VarChar),
                new SqlParameter("@Tra_ThanhPho",SqlDbType.NVarChar),      
                  new SqlParameter("@Tra_HuyenTinh", SqlDbType.NVarChar),
                new SqlParameter("@Tra_MaVung", SqlDbType.VarChar),
                new SqlParameter("@Tra_TenNuoc", SqlDbType.NVarChar),
                new SqlParameter("@PTThanhToanID", SqlDbType.Int),
                new SqlParameter("@Comment", SqlDbType.NVarChar),
                new SqlParameter("@AdminComment",SqlDbType.NVarChar),      
                new SqlParameter("@NgayLap",SqlDbType.DateTime),
                 new SqlParameter("@TongTien",SqlDbType.Float),     
                new SqlParameter("@TinhTrangID",SqlDbType.Int),      
                new SqlParameter("@error",SqlDbType.NVarChar,200),
                new SqlParameter("@MaHD",SqlDbType.NVarChar,20)};
            arrParam[0].Value = MaKH;
            arrParam[1].Value = Nhan_GioiTinh;
            arrParam[2].Value = Nhan_HoTen;
            arrParam[3].Value = Nhan_CongTy;
            arrParam[4].Value = Nhan_DiaChi;
            arrParam[5].Value = Nhan_DienThoai;
            arrParam[6].Value = Nhan_ThanhPho;
            arrParam[7].Value = Nhan_HuyenTinh;
            arrParam[8].Value = Nhan_MaVung;
            arrParam[9].Value = Nhan_TenNuoc;
            arrParam[10].Value = Tra_GioiTinh;
            arrParam[11].Value = Tra_HoTen;
            arrParam[12].Value = Tra_CongTy;
            arrParam[13].Value = Tra_DiaChi;
            arrParam[14].Value = Tra_DienThoai;
            arrParam[15].Value = Tra_ThanhPho;
            arrParam[16].Value = Tra_HuyenTinh;
            arrParam[17].Value = Tra_MaVung;
            arrParam[18].Value = Tra_TenNuoc;
            arrParam[19].Value = PTThanhToanID;
            arrParam[20].Value = Comment;
            arrParam[21].Value = AdminComment;
            arrParam[22].Value = NgayLap;
            arrParam[23].Value = TongTien;
            arrParam[24].Value = TinhTrangID;

            arrParam[25].Direction = ParameterDirection.Output;//Luu y cho nay    
            arrParam[26].Direction = ParameterDirection.Output;//Luu y cho nay  
            DB.ExeCuteStoreParam("HoaDon_Insert", arrParam);
            error = (arrParam[25].Value.ToString() + "" != "" ? arrParam[25].Value.ToString() : "");
            MaHD = (arrParam[26].Value.ToString() + "" != "" ? arrParam[26].Value.ToString() : "");
        }
        public void HoaDon_Delete(int HoaDonID, ref string error)
        {
            SqlParameter[] arrParam ={
                new SqlParameter("@HoaDonID", SqlDbType.Int),
                new SqlParameter("@error",SqlDbType.NVarChar,200)};
            arrParam[0].Value = HoaDonID;
            arrParam[1].Direction = ParameterDirection.Output;//Luu y cho nay    
            DB.ExeCuteStoreParam("HoaDon_Delete", arrParam);
            error = (arrParam[1].Value.ToString() + "" != "" ? arrParam[1].Value.ToString() : "");
        }
        public DataTable HoaDon_Search(int HoaDonID, int MaKH, int TinhTrangID, int PTThanhToanID)
        {
            SqlParameter[] arrParam ={
                new SqlParameter("@HoaDonID", SqlDbType.Int),
                new SqlParameter("@MaKH", SqlDbType.Int),
                new SqlParameter("@TinhTrangID", SqlDbType.Int),
                new SqlParameter("@PTThanhToanID", SqlDbType.Int)};
            arrParam[0].Value = HoaDonID;
            arrParam[1].Value = MaKH;
            arrParam[2].Value = TinhTrangID;
            arrParam[3].Value = PTThanhToanID;
            dt = DB.ViewTableParamStore("HoaDon_Search", arrParam);
            return dt;
        }
        public void HoaDon_UpdateTongTien(int HoaDonID, float TongTien)
        {
            SqlParameter[] arrParam ={
                new SqlParameter("@HoaDonID", SqlDbType.Int),
                new SqlParameter("@TongTien", SqlDbType.Float)};
            arrParam[0].Value = HoaDonID;
            arrParam[1].Value = TongTien;
            DB.ExeCuteStoreParam("HoaDon_UpdateTongTien", arrParam);
        }
        public DataTable HoaDon_ThongKeTinhTrang()
        {
            DataTable dt = DB.ViewTableParamStore("HoaDon_ThongKeTinhTrang");
            return dt;
        }
        public DataTable HoaDon_ViewOrder(int HoaDonID)
        {
            SqlParameter[] arrParam ={
                new SqlParameter("@HoaDonID", SqlDbType.Int)};
            arrParam[0].Value = HoaDonID;           
            DataTable dt = DB.ViewTableParamStore("HoaDon_View", arrParam);
            return dt;
        }
        public string HoaDon_Count()
        {
            string Count = DB.ExeCuteScalarParamStore("HoaDon_Count").ToString();
            return Count;
        }
        public DataTable HoaDon_New()
        {
            DataTable dt = DB.ViewTableParamStore("HoaDon_New");
            return dt;
        }
    }
}
